require("../css/register.less")
const CaptchaMini = require("../lib/captcha-mini")

document.ready(function () {
    //定义一个空值去接受验证码
    let count = ""
    let captcha1 = new CaptchaMini();
    captcha1.draw(document.querySelector('#captcha1'), r => {
        count=r
    });
    // console.log("注册");

    //获取dom
    let phoneInp = document.querySelector("#phoneInp")
    let captchaInp = document.querySelector("#captchaInp")
    let passwordInp = document.querySelector("#passwordInp")
    let passwordAgain = document.querySelector("#passwordAgain")
    let submitBtn = document.querySelector("#submitBtn")
    let tips = document.querySelector("#tips")


    //注册点击事件
    submitBtn.addEventListener("click", function () {
       
        //获取用户输入的值
        let phone = phoneInp.value //电话

        let captcha = captchaInp.value //验证码

        let password = passwordInp.value//密码

        let pwdAgain = passwordAgain.value//二次密码

        //判断, 用正则
        if (!window.$utils.isPhone(phone)) {
            tips.textContent = "您输入的手机号有误"
        } else if (captcha.toLowerCase() != count.toLowerCase()) {
            tips.textContent = "您输入的验证码有误"
        }else if(!window.$utils.isPassword(password)){
            tips.textContent = "您输入的密码有误"
        }else if(password!=pwdAgain){
            tips.textContent = "两次密码不一样"
        }else{
            // 清空
            tips.textContent = ""

            axios.post("http://139.9.177.51:8099/users/add",{
                "account":phone,
                "password":password
            }).then(function(res){
                 res=res.data
                if(res.status==0){
                    window.$utils.toast("√","注册成功")
                    window.setTimeout(function(){
                        window.location.href = "./login.html";
                    },3000)
                   
                }else if(res.status==5001){
                    window.$utils.toast("×","注册失败")
                }
            })
            .catch(function(err){
                console.log(err);
                
            })
            //发送ajax,请求注册
        }

    })

})